package com.hankol.newretail.adapter.cart

import android.annotation.SuppressLint
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.baselibrary.Constants
import com.baselibrary.extension.checkValue
import com.baselibrary.extension.toast
import com.baselibrary.manager.SensorsDataManager
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.baselibrary.utils.ARouterUtils.jump
import com.baselibrary.utils.DoubleClickHelper
import com.baselibrary.utils.TimeUtil
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hankol.newretail.R
import com.hankol.newretail.bean.ShopCartBean
import com.hankol.newretail.bean.ShopCartListBean
import com.hankol.newretail.callback.OnShopCartCallBack
import java.util.HashMap

/**
 * @author: 关忆北、
 * @date: 2022/8/5
 * @Des：一个店内的所有商品
 */
class ShopCartGroupAdapter : BaseQuickAdapter<ShopCartBean, BaseViewHolder>(R.layout.item_shop_cart_group) {
    var clickPos: Int = -1
    private var onShopCartCallBack: OnShopCartCallBack? = null
    var childAdapterList: ArrayList<ShopCartChildAdapter> = arrayListOf()//所有子商品
    fun setOnShopCartCallBack(onShopCartCallBack: OnShopCartCallBack) {
        this.onShopCartCallBack = onShopCartCallBack
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun convert(helper: BaseViewHolder, item: ShopCartBean) {
        val llCartGroup = helper.getView<LinearLayout>(R.id.ll_cart_group)
        val rvCartGroup = helper.getView<RecyclerView>(R.id.rv_cart_group)

        helper.setText(R.id.tv_cart_group_name, item.shopName)
        helper.getView<TextView>(R.id.tv_cart_group_name).setOnClickListener {
            val hashMap = HashMap<String, Any>()
            hashMap["id"] = "${item.shopId}"
            hashMap["dealerCode"] = "${item.dealerCode}"
            ARouterUtils.jump(ActivityRouter.WINE_CELLAR.WINCE_CELLAR_DETAIL, hashMap)
        }
        val cartChildAdapter = ShopCartChildAdapter()
        cartChildAdapter.setOnShopCartCallBack(onShopCartCallBack)
        rvCartGroup.adapter = cartChildAdapter
        cartChildAdapter.setNewData(item.mallCartSkuDtoList)
        childAdapterList.add(cartChildAdapter)
        if (isAllSelected(item.mallCartSkuDtoList)) {
            llCartGroup.isSelected = true
            helper.setImageResource(R.id.iv_cart_group, R.drawable.ic_shopcart_selected)
        } else {
            llCartGroup.isSelected = false
            helper.setImageResource(R.id.iv_cart_group, R.drawable.ic_shopcart_unselected)
        }
        cartChildAdapter.setOnItemChildClickListener { _, view, position ->
            clickPos = position
            when (view.id) {
                R.id.ll_shop_cart_det -> {
                    //删除该商品
                    try {
                        onShopCartCallBack?.delChildShop(helper.adapterPosition, position)
                    } catch (e: Exception) {
                        "操作失败".toast()
                    }
                }
                R.id.ll_shop_cart_reduce -> {
                    //数量-1
                    if (!DoubleClickHelper.isOnDoubleClick()) {
                        if (item.mallCartSkuDtoList[position].num > 1) {
                            onShopCartCallBack?.onShopReduce(helper.adapterPosition, position)
                        } else {
                            "最少购买一件".toast()
                        }
                    }
                }
                R.id.ll_shop_cart_plus -> {
                    //数量+1
                    if (!DoubleClickHelper.isOnDoubleClick()) {
                        if (item.mallCartSkuDtoList[position].num < item.mallCartSkuDtoList[position].maxNum) {
                            onShopCartCallBack?.onShopPlus(helper.adapterPosition, position)
                        } else {
                            "超出最大库存".toast()
                        }
                    }
                }
                R.id.ll_cart_child_select -> {
                    onShopCartCallBack?.onChildShopSelect(helper.adapterPosition, position)
                    //判断是否所有字条目被选中
                    if (isAllSelected(item.mallCartSkuDtoList)) {
                        llCartGroup.isSelected = true
                        helper.setImageResource(R.id.iv_cart_group, R.drawable.ic_shopcart_selected)
                    } else {
                        llCartGroup.isSelected = false
                        helper.setImageResource(R.id.iv_cart_group, R.drawable.ic_shopcart_unselected)
                    }
                }
                R.id.round_cart_child_cover, R.id.ll_cart_child_goods -> {
                    val map = HashMap<String, Any>()
                    map[Constants.GOODS_DETAIL_ID] = item.mallCartSkuDtoList[position].spuId
                    map[Constants.GOODS_DETAIL_SOURCE] = "购物车页"
                    jump(ActivityRouter.NEW_RETAIL.GOODS_DETAUILS, map)
                }
            }

        }
        llCartGroup.setOnClickListener {
            if (llCartGroup.isSelected) {
                //取消全部选中
                llCartGroup.isSelected = false
                cartChildAdapter.unSelectedAll()
                helper.setImageResource(R.id.iv_cart_group, R.drawable.ic_shopcart_unselected)
                onShopCartCallBack?.onGroupSelect(false, helper.adapterPosition)
            } else {
                //全部选中
                llCartGroup.isSelected = true
                cartChildAdapter.selectedAll()
                helper.setImageResource(R.id.iv_cart_group, R.drawable.ic_shopcart_selected)
                onShopCartCallBack?.onGroupSelect(true, helper.adapterPosition)
            }
        }
    }

    /**
     * 是否所有字条目被选中
     */
    fun isAllSelected(list: ArrayList<ShopCartListBean>): Boolean {
        val allowList: ArrayList<ShopCartListBean> = arrayListOf()
        list.forEach {
            if (it.status == 0) {
                allowList.add(it)
            }
        }

        if (allowList.isEmpty()) return false

        allowList.forEach {
            if (it.selected != 1) return false
        }
        return true
    }

    @SuppressLint("NotifyDataSetChanged")
    fun selectedGroupAll() {
        data.forEach { group ->
            group.mallCartSkuDtoList.forEach { child ->
                if (child.status == 0) {
                    child.selected = 1
                }
            }
        }
        notifyDataSetChanged()
    }

    @SuppressLint("NotifyDataSetChanged")
    fun unSelectedGroupAll() {
        data.forEach { group ->
            group.mallCartSkuDtoList.forEach { child ->
                if (child.status == 0) {
                    child.selected = 0
                }
            }
        }
        notifyDataSetChanged()
    }


}